import type { CourseCategoryCreateItem, CourseCategoryItem } from '@/types/courseCategory'
import http from './http'
import { apiMsgProp } from '@/utils/global'
import type { Pagination } from '@/types/global'

const CourseCategoryService = {
    /**
     * @function list 获取课程分类列表
     * @returns CourseCategoryItem[] 课程分类列表
     */
    list: async (per_page: number = 10000): Promise<{ items: CourseCategoryItem[]; pagination: Pagination }> => {
        const res = await http.get(`/course/category/list?per_page=${per_page}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        res.data.data.items.forEach((item: CourseCategoryItem) => {
            item.children = []
        })

        // 构建树形结构
        const itemMap = res.data.data.items.reduce((map: Record<number, CourseCategoryItem>, item: CourseCategoryItem) => {
            map[item.id] = item
            return map
        }, {})

        const treeData: CourseCategoryItem[] = []
        res.data.data.items.forEach((item: CourseCategoryItem) => {
            if (item.parent_id === 0) {
                treeData.push(item)
            } else {
                const parent = itemMap[item.parent_id]
                parent?.children?.push(item)
            }
        })

        return {
            items: treeData,
            pagination: res.data.data.pagination,
        }
    },
    /**
     * @function create 创建课程分类
     * @param data 课程分类创建信息
     */
    create: async (data: CourseCategoryCreateItem): Promise<CourseCategoryItem> => {
        const res = await http.post('/course/category/add', data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data.items
    },
    /**
     * @function update 更新课程分类
     * @param data 课程分类更新信息
     */
    update: async (data: CourseCategoryCreateItem): Promise<CourseCategoryItem> => {
        const res = await http.post(`/course/category/${data.id}/update`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data.items
    },
    /**
     * @function get 获取课程分类信息
     * @param id 课程分类ID
     */
    getById: async (id: number): Promise<CourseCategoryItem> => {
        const res = await http.get(`/course/category/${id}/info`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        return res.data.data.items
    },
    /**
     * @function delete 删除课程分类
     * @param id 课程分类ID
     */
    delete: async (id: number): Promise<void> => {
        const res = await http.delete(`/course/category/${id}`)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function sort 排序
     * @param cates 分类数组 id, sort_order
     */
    sort: async (cates: { id: number; sort_order: number }[]): Promise<void> => {
        const res = await http.post(`/course/category/sort`, cates)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
}

export default CourseCategoryService
